package com.dc.di.sf.dt;

import org.apache.axiom.om.OMElement;

import com.dc.di.sf.utils.OMUtils;

public class BusinessData implements DTObject<BusinessData> {

	public static final String TAG_BUSINESSDATA = "BusinessData";

	private DataStructure dataStructure;
	private DataSet dataSet;
	private Attachments attachments;

	public BusinessData() {

	}

	public DataStructure getDataStructure() {
		return dataStructure;
	}

	public void setDataStructure(DataStructure dataStructure) {
		this.dataStructure = dataStructure;
	}

	public DataSet getDataSet() {
		return dataSet;
	}

	public void setDataSet(DataSet dataSet) {
		this.dataSet = dataSet;
	}

	/**
	 * @return the attachments
	 */
	public Attachments getAttachments() {
		return attachments;
	}

	/**
	 * @param attachments
	 *            the attachments to set
	 */
	public void setAttachments(Attachments attachments) {
		this.attachments = attachments;
	}

	public OMElement toXML() {
		OMElement data = OMUtils.createOMElement(TAG_BUSINESSDATA);
		if (dataStructure != null)
			data.addChild(dataStructure.toXML());
		if (dataSet != null)
			data.addChild(dataSet.toXML());
		if (attachments != null)
			data.addChild(attachments.toXML());
		return data;
	}

	public BusinessData fromXML(OMElement data) {
		if (data != null) {
			this.dataStructure = new DataStructure().fromXML(data.getFirstChildWithName(OMUtils
					.createQName(DataStructure.TAG_DATASTRUCTURE)));
			this.dataSet = new DataSet(dataStructure).fromXML(data.getFirstChildWithName(OMUtils
					.createQName(DataSet.TAG_DATASET)));
			this.attachments = new Attachments().fromXML(data.getFirstChildWithName(OMUtils
					.createQName(Attachments.TAG_ATTACHMENTS)));
		}
		return this;
	}

	/* (non-Javadoc)
	 * @see com.dc.di.sf.dt.DTObject#toXMLString()
	 */
	public String toXMLString() {
		StringBuilder sb = new StringBuilder();
		sb.append(OMUtils.generateOMElementBeginString(TAG_BUSINESSDATA, Constants.NAMESPACE_CNEGD_PREFIX, Constants.NAMESPACE_CNEGD_URL));
		if (dataStructure != null)
			sb.append(dataStructure.toXMLString());
		if (dataSet != null)
			sb.append(dataSet.toXMLString());
		if (attachments != null)
			sb.append(attachments.toXMLString());
		sb.append(OMUtils.generateOMElementEndString(TAG_BUSINESSDATA));
		return sb.toString();
	}

}
